package lab6.systemCustom;

import java.util.Observable;
import java.util.Observer;

public class CourseOverdemandCheck implements Observer {

	/**
	 * Limita de studenti pentru determinarea suprasolitarii unui curs.
	 */
	public static final int OVERDEMAND_START_LIMIT = 5;

	/**
	 * Referita la obiectul BD.
	 */
	private DataBase dataBase;

	public CourseOverdemandCheck(DataBase objDataBase) {
		this.dataBase = objDataBase;
		// Subscribe to EV_CHECK_COURSE_OVERDEMAND event.
		EventBus.subscribeTo(EventBus.EV_CHECK_COURSE_OVERDEMAND, this);
	}

	/**
	 * Event handler of this course over demand check component. On receiving a
	 * register student event, the checking is done.
	 * 
	 * @param event
	 *            an event object. (caution: not to be directly referenced)
	 * @param param
	 *            a parameter object of the event. (to be cast to appropriate
	 *            data type)
	 */
	public void update(Observable event, Object param) {
		Course objCourse = this.dataBase.getCourseRecord((String) param);
		if (objCourse != null && objCourse.getRegisteredStudents().size() > CourseOverdemandCheck.OVERDEMAND_START_LIMIT) {
			EventBus.announce(EventBus.EV_SHOW, "Cursul " + (String) param + " este suprasolicitat.");
		} else {
			EventBus.announce(EventBus.EV_SHOW, "Cursul " + (String) param + " nu este deocamdata suprasolicitat.");
		}
	}

}